﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TMIS.DAL
{
    public class Teacher
    {
        private Model.ThesisDB db = new Model.ThesisDB();

        /// <summary>
        /// 根据教师姓名及密码查找该教师
        /// </summary>
        /// <param name="name">教师姓名</param>
        /// <param name="password">教师密码</param>
        /// <returns></returns>
        public int Search(string name, string password)
        {
            int guid = (from t in db.Teacher
                         where t.tName == name && t.tPassword == password
                         select t).Count();
            return guid;
        }
        public Guid Search(string name)
        {
            Guid guid = (from t in db.Teacher
                         where t.tName == name
                         select t.ID).FirstOrDefault();
            return guid;
        }
        /// <summary>
        /// 根据教师姓名及密码查找该教师对应的ID号
        /// </summary>
        /// <param name="name">教师姓名</param>
        /// <param name="password">教师密码</param>
        /// <returns></returns>
        public Guid Get(string name, string password)
        {
            Guid guid = (from t in db.Teacher
                        where t.tName==name && t.tPassword == password
                        select t.ID).First();
            return guid;
        }
        /// <summary>
        /// 根据教师姓名和教师密码查找该教师对应的ID号
        /// </summary>
        /// <param name="name">教师姓名</param>
        /// <param name="name">教师密码</param>
        /// <returns></returns>
        public Guid GetName(string name)
        {
            Guid guid = (from t in db.Teacher
                         where t.tName == name
                         select t.ID).FirstOrDefault();
            return guid;
        }

        /// <summary>
        /// 获取所有教师名字
        /// </summary>
        /// <returns></returns>
        public string[] GetName()
        {
            string[] name = (from t in db.Teacher
                             select t.tName).ToArray();
            return name;
        }

        /// <summary>
        /// 根据教师姓名和教师密码查找教师的详细信息
        /// </summary>
        /// <param name="name">教师姓名</param>
        /// <returns></returns>
        public Model.Teacher Get(string name)
        {
            Model.Teacher teacher = (from t in db.Teacher
                                     where t.tName == name
                                     select t).FirstOrDefault();
            return teacher;
        }

        /// <summary>
        /// 将新的教师实体数据写入到数据库中。
        /// </summary>
        /// <param name="teacher"></param>
        public void Add(Model.Teacher teacher)
        {
            db.Teacher.InsertOnSubmit(teacher);
            db.SubmitChanges();
        }

        /// <summary>
        /// 删除指定ID号对应的教师信息
        /// </summary>
        /// <param name="guid"></param>
        public void Del(Guid guid)
        {
            var teacher = db.Teacher.Single(e => e.ID == guid);
            db.Teacher.DeleteOnSubmit(teacher);
            db.SubmitChanges();
        }
        /// <summary>
        /// 根据指定的ID号查找对应的教师名称
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public string GetName(Guid? guid)
        {
            string name = (from t in db.Teacher
                           where t.ID == guid
                           select t.tName).FirstOrDefault();
            return name;
        }


    }
}
